//
//  BaseTableViewController.swift
//  iOSProject-XX
//
//  Created by LeeXu on 2020/7/16.
//

import UIKit
import DZNEmptyDataSet
import MJRefresh
import Alamofire
import HandyJSON

class BaseTableViewController: BaseViewController {

    var style:UITableView.Style = .plain
    var page:Int = 1
    var models:Array<Any>?  //DataArray 
    @IBOutlet var tableView:UITableView?
    convenience init(style:UITableView.Style) {
        self.init()
        self.style = style
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        if self.tableView == nil {
            self.tableView = UITableView(frame: .zero, style: self.style)
            self.tableView?.backgroundColor = .white
            self.tableView?.showsVerticalScrollIndicator = false
            self.tableView?.showsHorizontalScrollIndicator = false
            self.tableView?.frame = self.view.bounds
            self.tableView?.separatorStyle = .none
            self.tableView?.autoresizingMask = [.flexibleWidth , .flexibleHeight]
            self.view.addSubview(self.tableView!)
        }
        self.tableView?.emptyDataSetSource = self
        self.tableView?.emptyDataSetDelegate = self
        
        self.tableView?.mj_header = MJRefreshNormalHeader(refreshingBlock: { [weak self] in
            self?.page = 1
            self?.fetchData()
        })
        
        self.tableView?.mj_footer = MJRefreshBackFooter(refreshingBlock: { [weak self] in
            self?.page += 1
            self?.fetchData()
        })

        self.tableView?.mj_header?.beginRefreshing()
    }
    
    func  requestMethod() -> HTTPMethod  {
        return HTTPMethod.get
    }
    
    func requestAPI() -> String {
         return ""
    }
    
    func requestParameters() -> [String:Any]? {
        return nil
    }
}
 
extension BaseTableViewController {
    
    func fetchData() {
        
        AppRequestAPI.requestBanner(successHandler: { (data) in
            
    
        }) { (error) in
            self.page -= 1
            self.tableView?.mj_header?.endRefreshing()
            self.tableView?.mj_footer?.endRefreshing()
        }
    }
}

extension BaseTableViewController : DZNEmptyDataSetSource {
    func title(forEmptyDataSet scrollView: UIScrollView!) -> NSAttributedString! {
        return NSAttributedString(string: "Title")
    }
    
    func description(forEmptyDataSet scrollView: UIScrollView!) -> NSAttributedString! {
         return NSAttributedString(string: "Description")
    }
    func image(forEmptyDataSet scrollView: UIScrollView!) -> UIImage! {
         return UIImage(named: "timg.jpeg")
    }
    func buttonTitle(forEmptyDataSet scrollView: UIScrollView!, for state: UIControl.State) -> NSAttributedString! {
         return NSAttributedString(string: "点击登录")
    }
}
extension BaseTableViewController: DZNEmptyDataSetDelegate {
    func emptyDataSet(_ scrollView: UIScrollView!, didTap button: UIButton!) {
         
    }
     
}
